package com.ebupt.migu.music.popup.controller;

import com.ebupt.migu.common.pojo.Topic;
import com.ebupt.migu.music.commons.constants.ResultMsg;
import com.ebupt.migu.music.popup.dto.ActionListDTO;
import com.ebupt.migu.music.popup.dto.ActivityDetailDTO;
import com.ebupt.migu.music.popup.dto.ActivityHitDTO;
import com.ebupt.migu.music.popup.dto.AdsDTO;
import com.ebupt.migu.music.popup.service.ActionListService;
import com.ebupt.migu.music.popup.service.ActivityService;
import com.ebupt.migu.music.popup.service.AdsService;
import lombok.AllArgsConstructor;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Optional;

/**
 * 服务云全局弹窗接口  控制器
 *
 * @author wangzhu
 * @since 2021-11-17
 */
@RestController
@AllArgsConstructor
@RequestMapping("api")
public class ActivityController {
    @Autowired
    ActivityService activityService;
    @Autowired
    AdsService adsService;
    @Autowired
    ActionListService actionListService;

    /**
     * 获取活动列表
     */
    @PostMapping("/list")
    public ResultMsg list(@Valid @RequestBody ActionListDTO actionListDTO) {
        // 同步外部系统的requestId替换自己生成的
        Optional.ofNullable(actionListDTO.getExtra().get(Topic.REQUEST_ID_KEY)).ifPresent(e -> MDC.put(Topic.REQUEST_ID_KEY, e));
        return actionListService.getActionList(actionListDTO);
    }

    /**
     * 获取活动详情
     */
    @PostMapping("/detail")
    public ResultMsg detail(@Valid @RequestBody ActivityDetailDTO activityDetailDTO) {
        return activityService.getDetail(activityDetailDTO);
    }


    /**
     * 活动触发成功上报
     */
    @PostMapping("/hit")
    public ResultMsg hit(@Valid @RequestBody ActivityHitDTO activityHitDTO) {
        return activityService.getHit(activityHitDTO);
    }

    /**
     * 批量获取活动详情
     */
    @PostMapping("/batch")
    public ResultMsg batch(@Valid @RequestBody ActivityDetailDTO activityDetailDTO) {
        return activityService.getBatch(activityDetailDTO);
    }

    /**
     * 判定当前用户是否需要弹出广告
     *
     * @param
     * @return
     */
    @PostMapping("/ads")
    public ResultMsg ads(@Valid @RequestBody AdsDTO ads) {
        return adsService.ads(ads);
    }
}
